<template>
  <BaseDialog
    :title="(!props.item.opinionKeyId ? '新增' : '编辑') + '舆情词'"
    :handleCancel="handleCancel"
    :dialogVisible="props.dialogVisible"
  >
    <BaseForm
      labelWidth="100px"
      type="rule"
      :handleOk="handleOk"
      :handleCancel="handleCancel"
      :items="formItems"
      v-model:form="ruleForm"
      :rules="rules"
      :refresh="handleSearch"
    >
    </BaseForm>
  </BaseDialog>
</template>

<script setup lang="ts">
import { contentManagementService } from '@/api/contentManagement';
import store from '@/store';
const cateList = computed(() => store.getters.cateList);

const props = defineProps({
  dialogVisible: {
    type: Boolean,
  },
  item: {
    type: Object,
  },
  handleSearch: {
    type: Function,
  },
  searchParams: {
    type: Object,
  },
});
const emit = defineEmits(['update:dialogVisible']);

const formItems = ref([
  {
    name: '所属分类',
    key: 'typeId',
    type: 'select',
    options: cateList,
  },
  {
    name: '舆情词',
    key: 'opinionKey',
    type: 'input',
    maxLength: 20,
  },
  {
    name: '类型',
    key: 'level',
    type: 'select',
    options: [
      { label: '重要舆情词', value: 1 },
      { label: '一般舆情词', value: 2 },
    ],
  },
  {
    name: '状态',
    key: 'isDisable',
    type: 'switch',
    activeValue: 0,
    inactiveValue: 1,
  },
]);
const rules = reactive({
  opinionKey: [{ required: true, message: '请输入', trigger: 'change' }],
  typeId: [{ required: true, message: '请选择', trigger: 'change' }],
  level: [{ required: true, message: '请选择', trigger: 'change' }],
  isDisable: [{ required: true, message: '请选择', trigger: 'change' }],
});
const ruleForm = ref({
  opinionKeyId: '',
  typeId: '',
  opinionKey: '',
  level: '',
  isDisable: 0,
});
const handleOk = () => {
  if (props.item.opinionKeyId) {
    return contentManagementService.libOpinionKeyUpdate({
      data: { ...ruleForm.value },
    });
  } else {
    return contentManagementService.libOpinionKeySaveBatch({
      data: [{ ...ruleForm.value }],
    });
  }
};

const handleCancel = () => {
  emit('update:dialogVisible', false);
};
onMounted(() => {
  if (props.item.opinionKeyId) {
    ruleForm.value.typeId = props.item.typeId;
    ruleForm.value.opinionKey = props.item.opinionKey;
    ruleForm.value.isDisable = props.item.isDisable;
    ruleForm.value.opinionKeyId = props.item.opinionKeyId;
    ruleForm.value.level = props.item.level;
  } else {
    ruleForm.value.typeId = props.searchParams.typeId;
  }
});
</script>

<style lang="scss" scoped></style>
